package com.cspg.ssss.controller;

import com.cspg.ssss.service.KanBanService;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/kanban")
public class KanBanController {
    @Autowired
    private KanBanService kanBanService;

    @RequestMapping(value = "/selectsj", method = RequestMethod.GET)
    public ResultVo selectsj(String creattime,String endtime) {
        ResultVo retVo = null;
        Map map = new HashMap();
        int wentizs = kanBanService.selectwemtizs(creattime, endtime);  //查询总问题数
        int wentizyanshou = kanBanService.selectzyanshou(creattime, endtime);//查询问题总验收数
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String zwcl = df.format((double) wentizyanshou / wentizs * 100);//总完成率
        map.put("wentizs", wentizs);
        map.put("zwcl", zwcl);

        //单位完成率开始
        List<Map> selectdwwentizs = kanBanService.selectdwwentizs(creattime, endtime);//各单位问题总数列表
        List<Map> selectdwyanshouzs = kanBanService.selectdwyanshouzs(creattime, endtime);//各单位验收总数
        List list = new ArrayList();

        for (Map selectdwwentiz : selectdwwentizs) {
            boolean con = true;
            String dwname2 = selectdwwentiz.get("dwmc").toString();
            for (Map selectdwyanshouz : selectdwyanshouzs) {
                String dwname = selectdwyanshouz.get("dwmc").toString();
                if (dwname.equals(dwname2)) {
                    String dwys = selectdwyanshouz.get("sl").toString();
                    String dwys2 = selectdwwentiz.get("sl").toString();
                    //单位总验收
                    int a = Integer.parseInt(dwys);
                    //单位问题总数
                    int b = Integer.parseInt(dwys2);
                    String dwzwcl = df.format((double) a / b * 100);//总完成率
                    Map dwmap = new HashMap();
                    dwmap.put(dwname, dwzwcl);
                    list.add(dwmap);
                    con = false;
                    break;
                }
            }
            if (con) {
                Map dwmap = new HashMap();
                dwmap.put(dwname2, "0");
                list.add(dwmap);
            }
        }
        map.put("dwwentizs", selectdwwentizs);
        map.put("dwlist", list);
        //单位完成率结束

        //单位主动暴露完成率开始
        List<Map> selectdwzdblzs = kanBanService.selectdwzdblzs(creattime, endtime);//各单位问题总数列表
        List<Map> selectdwzdblyanshouzs = kanBanService.selectdwzdblyanshouzs(creattime, endtime);//各单位验收总数
        List list2 = new ArrayList();
        for (Map selectdwzdblz : selectdwzdblzs) {
            String dwname2 = selectdwzdblz.get("dwmc").toString();
            boolean con = true;
            for (Map selectdwzdblyanshouz : selectdwzdblyanshouzs) {
                String dwname = selectdwzdblyanshouz.get("dwmc").toString();
                if (dwname.equals(dwname2)) {
                    String dwys = selectdwzdblyanshouz.get("sl").toString();
                    String dwys2 = selectdwzdblz.get("sl").toString();
                    //单位总验收
                    int a = Integer.parseInt(dwys);
                    //单位问题总数
                    int b = Integer.parseInt(dwys2);
                    String dwzwcl = df.format((double) a / b * 100);//总完成率
                    Map dwblmap = new HashMap();
                    dwblmap.put(dwname, dwzwcl);
                    list2.add(dwblmap);
                    con = false;
                    break;
                }
            }
            if (con) {
                Map dwblmap = new HashMap();
                dwblmap.put(dwname2, "0");
                list2.add(dwblmap);
            }
        }
        map.put("dwzdblzs", selectdwzdblzs);
        map.put("dwzdbllist", list2);
        //单位主动暴露完成率结束

        //各单位公司发现问题开始
        List<Map> selectgdwgsfaxianzs = kanBanService.selectgdwgsfaxianzs(creattime, endtime);//各单位问题总数列表
        List<Map> selectgdwgsfaxianyanshouzs = kanBanService.selectgdwgsfaxianyanshouzs(creattime, endtime);//各单位验收总数
        List list3 = new ArrayList();
        for (Map selectgdwgsfaxianyanshouz : selectgdwgsfaxianyanshouzs) {
            String dwname = selectgdwgsfaxianyanshouz.get("dwmc").toString();
            for (Map selectgdwgsfaxianz : selectgdwgsfaxianzs) {
                String dwname2 = selectgdwgsfaxianz.get("dwmc").toString();
                if (dwname.equals(dwname2)) {
                    String dwys = selectgdwgsfaxianyanshouz.get("sl").toString();
                    String dwys2 = selectgdwgsfaxianz.get("sl").toString();
                    //单位总验收
                    int a = Integer.parseInt(dwys);
                    //单位问题总数
                    int b = Integer.parseInt(dwys2);
                    String dwzwcl = b == 0 ? "0" : df.format((double) a / b * 100);//总完成率
                    Map dwgsblmap = new HashMap();
                    dwgsblmap.put(dwname, dwzwcl);
                    list3.add(dwgsblmap);
                    break;
                }
            }
        }
        map.put("dwgsfaxianwentizs", selectgdwgsfaxianzs);
        map.put("dwgszdbllist", list3);
        //各单位公司发现问题结束
        retVo = ResultVoUtil.success("查询成功", map, "400");
        return retVo;
    }


    /**
     * 查询单位的数据
     *
     * @param depart
     * @param datalist
     * @return
     */
    public List findAlllInData(Map depart, List<Map> datalist) {
        List result = new ArrayList();
        for (Map data : datalist) {
            if (depart.get("department_name").equals(data.get("department_name").toString())) {
                result.add(data);
            }

        }
        return result;
    }

    public List findAlllInData1(Map depart, List<Map> datalist) {
        List result = new ArrayList();
        for (Map data : datalist) {
            if (depart.get("department_name").equals(data.get("department_name").toString())) {
                result.add(data);
            }

        }
        return result;
    }

}
